package com.party.admin.biz.photo;

import com.party.admin.biz.file.FileBizService;
import com.party.common.utils.StringUtils;
import com.party.core.model.photo.PhotoProject;
import com.party.core.model.photo.PhotosImgInfo;
import com.party.core.service.cos.CosBizService;
import com.party.core.service.file.IImgResourceService;
import com.party.core.service.photo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * 相册业务处理
 *
 * @author yifeng
 * @date 2018/7/16 0016
 * @time 下午 18:45
 */
@Service
public class PhotoProjectBizService {
    @Autowired
    private FileBizService fileBizService;
    @Autowired
    private CosBizService cosBizService;
    @Autowired
    private IPhotoProjectService photoProjectService;
    @Autowired
    private IPhotosImgInfoService photosImgInfoService;
    @Autowired
    private IPhotosImgTagService photosImgTagService;
    @Autowired
    private IPhotosMemberOperateService photosMemberOperateService;
    @Autowired
    private IPhotosMemberOperateDetailService photosMemberOperateDetailService;
    @Autowired
    private IImgResourceService imgResourceService;

    Logger logger = LoggerFactory.getLogger(getClass());


    /**
     * 删除相册
     *
     * @param photoId 相册编号
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean deletePhotoBiz(String photoId) {
        boolean endResult = true;
        String memberId = null;
        try {
            if (StringUtils.isEmpty(photoId)) {
                endResult = false;
                return endResult;
            }
            PhotoProject photoProject = photoProjectService.get(photoId);
            if (photoProject == null) {
                endResult = false;
                return endResult;
            }
            memberId = photoProject.getMemberId();
            List<PhotosImgInfo> photosImgInfos = photosImgInfoService.findByPhotoId(photoId);
            for (PhotosImgInfo photosImgInfo : photosImgInfos) {
                if (StringUtils.isNotEmpty(photosImgInfo.getImgResourceId())) {
                    imgResourceService.delete(photosImgInfo.getImgResourceId());
                }
            }
            photosImgInfoService.deleteByPhotoId(photoId);
            photosMemberOperateDetailService.deleteByPhotoId(photoId);
            photosMemberOperateService.deleteByPhotoId(photoId);
            photosImgTagService.deleteByPhotoId(photoId);
            photoProjectService.delete(photoId);
        } catch (Exception e) {
            endResult = false;
            logger.error("删除相册失败", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        } finally {
            if (endResult) {
                try {
                    String filePath = fileBizService.getUploadCIPath(memberId, "photo");
                    String detailPath = CosBizService.getDeleteQrCodePath(filePath, photoId);
                    cosBizService.deleteFile(detailPath);
                } catch (Exception e) {
                    logger.error("删除相册二维码失败", e);
                }
            }
        }
        return endResult;
    }
}
